package com.shelpe.services.sinbad.service.simbaproxy;

import java.util.Date;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.shelpe.services.sinbad.service.simbaproxy.LexiconClient.LexiconClientFallback;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;

@FeignClient(name = "${feign.lexiconserver.name}", url = "${feign.lexiconserver.url}", fallback = LexiconClientFallback.class)
public interface LexiconClient {

	@RequestMapping(method = RequestMethod.GET, path = "/lexiconservice/categories/{category_id}/insights", consumes = "application/json;charset=UTF-8")
	public EffectMetricResult findCategoryInsightMetric(@PathVariable("category_id") long categoryID);
	
	@RequestMapping(method = RequestMethod.GET, path = "/lexiconservice/users/@lastSyncTime", consumes = "application/json;charset=UTF-8")
	public LastSyncDto findLastSyncTime(@RequestParam(name = "user_id", required = false, defaultValue = "0") long userID,
			@RequestParam(name = "campaign_id", required = false, defaultValue = "0") long campaignID,
			@RequestParam(name = "adgroup_id", required = false, defaultValue = "0") long adgroupID);

	@Data
	@EqualsAndHashCode(callSuper = true)
	public static class EffectMetricResult extends SimbaProxyBaseDto {
		private boolean found = false;
		private EffectMetricDto metric;
	}

	@Data
	public class EffectMetricDto {
		private int cost;// 花费（分）
		private float cpc;// 平均点击花费
	}
	
	@Data
	public static class LastSyncDto {		
		private boolean rptCampaignSync = false;		
		private boolean rptAdgroupSync = false;				
		private boolean rptKeywordsSync = false;
		private Date rptKeywordsSyncTime;
	}

	@Service
	@Slf4j
	public static class LexiconClientFallback implements LexiconClient {
		@Override
		public EffectMetricResult findCategoryInsightMetric(long categoryID) {
			log.error("fallback findCategoryInsightMetric: {}", categoryID);
			SimbaProxyBaseDto.Error error = new SimbaProxyBaseDto.Error(500, "fallback");
			EffectMetricResult res = new EffectMetricResult();
			res.setError(error);
			return res;
		}

		@Override
		public LastSyncDto findLastSyncTime(long userID, long campaignID, long adgroupID) {
			log.error("fallback find last sync time : {},{},{}",userID,campaignID,adgroupID);
			return new LastSyncDto();
		}
	}
}
